import logging

from flask import Flask
from flask_appbuilder import AppBuilder, SQLA
from .index import MyIndexView
from flask_appbuilder.menu import Menu
from .sec import MySecurityManager

"""
 Logging configuration
"""

logging.basicConfig(format="%(asctime)s:%(levelname)s:%(name)s:%(message)s")
logging.getLogger().setLevel(logging.DEBUG)

app = Flask(__name__)
app.config.from_object("config")
db = SQLA(app)
appbuilder = AppBuilder(app, db.session, indexview=MyIndexView, security_manager_class=MySecurityManager,
                        base_template='mybase.html')

from flask_login import logout_user
from flask_socketio import SocketIO

socketio = SocketIO()
socketio.init_app(app, cors_allowed_origins='*')

name_space = '/dcenter'


@socketio.on('connect', namespace=name_space)
def connected_user():
    print('client connected')


@socketio.on('disconnect',namespace=name_space)
def disconnect_msg():
    print('client disconnected')


# from flask_migrate import Migrate
# migrate = Migrate(app,db)

"""
from sqlalchemy.engine import Engine
from sqlalchemy import event

#Only include this for SQLLite constraints
@event.listens_for(Engine, "connect")
def set_sqlite_pragma(dbapi_connection, connection_record):
    # Will force sqllite contraint foreign keys
    cursor = dbapi_connection.cursor()
    cursor.execute("PRAGMA foreign_keys=ON")
    cursor.close()
"""

from . import views
